package com.banzhi.shop.fragment;

import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.banzhi.shop.R;

import com.banzhi.shop.adapter.CommonAdapter;
import com.banzhi.shop.entity.CollegeShouyi.CollegeShouyiBean;
import com.banzhi.shop.entity.CollegeShouyi.CollegeShouyiCallBack;
import com.banzhi.shop.entity.CollegeShouyi.CollegeShouyiData;
import com.banzhi.shop.entity.CollegeShouyi.CollegeShouyiImpl;

import com.banzhi.shop.utils.ASUtils;
import com.banzhi.shop.utils.DateUtil;
import com.banzhi.shop.utils.EndlessRecyclerOnScrollListener;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.LogUtils;
import com.banzhi.shop.utils.NoTokenUtils;
import com.banzhi.shop.utils.NumberUtils;
import com.banzhi.shop.utils.ShareUtils;
import com.banzhi.shop.utils.ToastUtils;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/5/12.
 */

public class College_YifaFragment extends Fragment implements CollegeShouyiCallBack, SwipeRefreshLayout.OnRefreshListener {
    private CollegeShouyiImpl collegeShouyiModel;


    private SwipeRefreshLayout refreshLayout;
    private RecyclerView recyclerView;

    private int page = 1;
    private List<CollegeShouyiBean> collegeShouyiBean = new ArrayList<>();
    private CommonAdapter<CollegeShouyiBean> adapter;
    private LinearLayoutManager LayoutManager;
    private RelativeLayout rlnodata;
    private View view;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_collegeshouyi_daifa, container, false);
        setinits(view);
        setlisteners();
        collegeShouyiModel = new CollegeShouyiImpl();
        LoadingDialogUtils.showProgress(getActivity(), "正在加载,请稍后~");
        collegeShouyiModel.getData(page, "1", this);
        return view;
    }

    private void setinits(View view) {
        refreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.srl_listview);
        refreshLayout.setColorSchemeResources(R.color.themeColor);
        refreshLayout.setProgressBackgroundColorSchemeResource(R.color.colorWhite);
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview);
        LayoutManager = new LinearLayoutManager(getActivity());
        LayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(LayoutManager);
        rlnodata = (RelativeLayout) view.findViewById(R.id.rl_nodata);

    }

    private void setlisteners() {

        refreshLayout.setOnRefreshListener(this);

        recyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener(LayoutManager) {
            @Override
            public void onLoadMore() {
                page++;
                LoadingDialogUtils.showProgress(getActivity(), "正在加载,请稍后~");
                collegeShouyiModel.getData(page, "0", College_YifaFragment.this);
            }
        });

    }

    @Override
    public void ongetDataSuccess(Object object) {
        LogUtils.d("1","DataSuccess");
        final List<CollegeShouyiBean> bean = (List<CollegeShouyiBean>) object;
        LogUtils.d("1","bean");
        if (page == 1) {
            collegeShouyiBean.clear();
        } else {
            if (bean.size() == 0 || bean == null ) {
                return;
            }
        }
        if (bean != null && bean.size()>0 ) {
            collegeShouyiBean.addAll(bean);
        }
        if (collegeShouyiBean.size() == 0||bean == null) {
            rlnodata.setVisibility(View.VISIBLE);//暂无数据显示
        } else {
            rlnodata.setVisibility(View.GONE);
        }

        if (adapter == null ) {
            adapter = new CommonAdapter<CollegeShouyiBean>(getActivity(), R.layout.item_collegeshouyi_daifa, collegeShouyiBean) {
                @Override
                public void convert(ViewHolder holder, final CollegeShouyiBean bean, int position) {
                    holder.setText(R.id.tv_ordernum, bean.getOrderSn());
                    holder.getTextView(R.id.tv_fuzhi).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            ShareUtils.copyLianjie(getActivity(), bean.getOrderSn());
                        }
                    });
                    holder.setText(R.id.tv_title, bean.getGoodsName());
                    holder.setText(R.id.tv_buy_time, bean.getPeriod());
                    holder.setText(R.id.tv_xiaofei, bean.getNickname());
                    holder.setText(R.id.tv_lirun1, bean.getAgentIncome());

                    holder.setText(R.id.tv_order_pay_time, DateUtil.getTimeData(bean.getPayTime()));

                    if (ASUtils.isCanUse(bean.getManageIncome())) {
                        holder.setText(R.id.tv_lirun1, NumberUtils.DecimalFormat(bean.getManageIncome()));
                    }

                }
            };
            recyclerView.setAdapter(adapter);
        } else {
            adapter.notifyDataSetChanged();
        }
        if (refreshLayout.isRefreshing()) {
            refreshLayout.setRefreshing(false);
        }
    }


    @Override
    public void ongetDataError(Object object) {
        if (refreshLayout.isRefreshing()) {
            refreshLayout.setRefreshing(false);
        }
        JSONObject jsonObject = (JSONObject) object;
        NoTokenUtils.loginOut(getActivity(), jsonObject);

        ToastUtils.makeToastShort(jsonObject.optString("msg"));

    }

    /*@Override
    public void ongetNullData(Object object) {
        if (object.toString()=="null") {
            rlnodata.setVisibility(View.VISIBLE);//暂无数据显示
        } else {
            rlnodata.setVisibility(View.GONE);
        }
    }*/


    @Override
    public void onRefresh() {
        LoadingDialogUtils.showProgress(getActivity(), "正在加载,请稍后~");
        page = 1;
        collegeShouyiModel.getData(page, "1", this);

    }
}
